package services

import (
	"errors"
	"hft/Database/admin"
	"hft/global"
)

type LouPanTeSe struct{}

func (*LouPanTeSe) Create(data *admin.LouPanTeSe) (err error) {
	res := global.OC_DB.Where("name = ?", data.Name).FirstOrCreate(&data)

	if res.Error != nil {
		return errors.New("未知错误500")
	}

	if res.RowsAffected == 0 {
		return errors.New("该特色已存在")
	}

	return nil
}

func (*LouPanTeSe) Update(data *admin.LouPanTeSe) (err error) {

	err = global.OC_DB.Model(&admin.LouPanTeSe{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
		"name":           data.Name,
		"show_in_search": data.ShowInSearch,
	}).Error

	if err != nil {
		return errors.New("未知错误500")
	}

	return nil
}

func (*LouPanTeSe) List() (list []*admin.LouPanTeSe, err error) {
	var teses []*admin.LouPanTeSe

	global.OC_DB.Order("sort desc").Find(&teses)

	return teses, nil
}

func (*LouPanTeSe) Delete(ids []int) (err error) {
	var list []*admin.LouPanTeSe

	global.OC_DB.Find(&list, ids)

	global.OC_DB.Unscoped().Delete(&list)

	return nil
}

var LouPanTeSeService = new(LouPanTeSe)
